package com.xihu.credit.util;

import java.util.HashMap;
import java.util.Map;

public class VerificationCodeCache {
    private static final Map<String, String> codeMap = new HashMap<>();
    private static final Map<String, Long> timeMap = new HashMap<>();
    private static final long EXPIRE_TIME = 10 * 60 * 1000; // 10分钟有效期

    public static void store(String email, String code) {
        codeMap.put(email, code);
        timeMap.put(email, System.currentTimeMillis());
    }

    public static boolean verify(String email, String code) {
        if (!codeMap.containsKey(email)) return false;
        long now = System.currentTimeMillis();
        if (now - timeMap.get(email) > EXPIRE_TIME) {
            codeMap.remove(email);
            timeMap.remove(email);
            return false;
        }
        return codeMap.get(email).equals(code);
    }
}

